package com.aditya.personalexpense.adapters;

import java.util.Vector;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aditya.personalexpense.R;
import com.aditya.personalexpense.dataaccess.PayModeDA;
import com.aditya.personalexpense.dataobject.PayModeDO;

public class PayModeAdapter extends BaseAdapter
{
	private Context context;
	private Vector<PayModeDO> vecPayModeDO;
	private boolean setViewIcon = true;
	private boolean setViewClicable = true;
	
	public PayModeAdapter(Context context, Vector<PayModeDO> vecPayModeDO)
	{
		this.context = context;
		this.vecPayModeDO = vecPayModeDO;
	}
	
	public void refreshList(Vector<PayModeDO> vecPayModeDO)
	{
		this.vecPayModeDO = vecPayModeDO;
		notifyDataSetChanged();
	}
	
	public void setViewIcon(boolean setViewIcon)
	{
		this.setViewIcon = setViewIcon;
	}
	
	public void setViewClicable(boolean setViewClicable)
	{
		this.setViewClicable = setViewClicable;
	}
	
	@Override
	public int getCount() {
		return vecPayModeDO.size();
	}

	@Override
	public PayModeDO getItem(int position) {
		return vecPayModeDO.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) 
	{
		convertView = LayoutInflater.from(context).inflate(R.layout.currency_item, null);
		
		TextView tvItemName = (TextView) convertView.findViewById(R.id.tvItemName);
		ImageView imageView = (ImageView) convertView.findViewById(R.id.ivItem);
		tvItemName.setText(vecPayModeDO.get(position).name);
		if(setViewIcon)
			imageView.setVisibility(View.VISIBLE);
		else
			imageView.setVisibility(View.GONE);
		
		if(setViewClicable)
		{
			convertView.setOnClickListener(new OnClickListener() 
			{
				@Override
				public void onClick(View v) 
				{
			        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
					LinearLayout view = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.add_paymode_dialog, null); 
					alertDialog.setView(view);
					final AlertDialog dialog = alertDialog.create();
					final TextView tvtitle = (TextView) view.findViewById(R.id.tvTitle);
					tvtitle.setText("Modify PayMode");
					final EditText edtPayModeName = (EditText) view.findViewById(R.id.edtPayModeName);
					
					final PayModeDO payModeDO = new PayModeDO();
					
					edtPayModeName.setText(vecPayModeDO.get(position).name);
					
					
					Button btnOk = (Button) view.findViewById(R.id.btnOk);
					Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
					btnOk.setOnClickListener(new OnClickListener() 
					{
						@Override
						public void onClick(View v) 
						{
							if(edtPayModeName.getText().toString().equalsIgnoreCase(""))
							{
								Toast.makeText(context, "Please enter the complete information!", Toast.LENGTH_SHORT).show();
							}
							else
							{
								final String currency = edtPayModeName.getText().toString();
							
								new Thread(new Runnable() {
									
									@Override
									public void run() 
									{
										
										payModeDO.name = currency;
										payModeDO.pid = vecPayModeDO.get(position).pid;
										
										new PayModeDA().updatePayMode(payModeDO);
												dialog.dismiss();
												refreshList(new PayModeDA().getAllPayModes());
									}
								}).start();
							}
						}
					});
					btnCancel.setOnClickListener(new OnClickListener() 
					{
						
						@Override
						public void onClick(View v) 
						{
							dialog.dismiss();
						}
					});
					dialog.show();
						
					}
				});
		}

		return convertView;
	}
	
}
